package com.tengju.data.application.profit;

import com.tengju.data.domain.model.alert.AlertService;
import com.tengju.data.domain.model.report.AchievementBonusLogRepository;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;

/**
 * @author chenhang
 * @Date 2021/6/22
 */
@Service
@RequiredArgsConstructor(onConstructor_ = {@Autowired})
@Slf4j
public class BonusLogArchiveService {

    private final AchievementBonusLogRepository bonusLogRepository;

    private static final String ARCHIVE_TABLE_PREFIX = "data_achievement_bonus_log";

    @Value("${bonus.log.archive.interval}")
    private Integer archiveInterval;

    public void archive(LocalDateTime logTime, Integer month) {
        LocalDateTime now = LocalDateTime.now();
        if (now.minusDays(archiveInterval).isBefore(logTime)) {
            log.info("不在归档间隔内：logTime:{}",logTime);
            return;
        }
        String archiveTableName = ARCHIVE_TABLE_PREFIX + "_" + month;
        bonusLogRepository.archive(logTime,archiveTableName);
        bonusLogRepository.deleteWithLogTime(logTime);
        log.info("归档记录：logTime:{},month:{}",logTime,month);
    }
}
